{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1a9ae0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21525326",
   "metadata": {},
   "source": [
    "### 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8bb1b532",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(\"互联网公司股票.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b5aca981",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>数据项</th>\n",
       "      <th>数据值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-10-10</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>收盘</td>\n",
       "      <td>103.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-10-10</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>开盘</td>\n",
       "      <td>100.79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-10-10</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>高</td>\n",
       "      <td>104.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-10-10</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>低</td>\n",
       "      <td>100.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-10-10</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>交易量</td>\n",
       "      <td>3.56</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司  数据项     数据值\n",
       "0  2019-10-10  BIDU   收盘  103.85\n",
       "1  2019-10-10  BIDU   开盘  100.79\n",
       "2  2019-10-10  BIDU    高  104.74\n",
       "3  2019-10-10  BIDU    低  100.26\n",
       "4  2019-10-10  BIDU  交易量    3.56"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "295a4ffe",
   "metadata": {},
   "source": [
    "### 透视数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92813e39",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pivot = pd.pivot_table(df,\n",
    "                          index=[\"日期\", \"公司\"], \n",
    "                          columns=\"数据项\", \n",
    "                          values=\"数据值\")\n",
    "\n",
    "df_pivot"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35726962",
   "metadata": {},
   "source": [
    "### 存储到结果表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca5e3843",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pivot.to_excel(\"透视结果表.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c7f415f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
